package com.ruoyi.article.service.impl;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.see.domain.ArticleSee;
import com.ruoyi.see.service.IArticleSeeService;
import com.ruoyi.system.domain.SysUserOnline;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.article.mapper.MyArticleMapper;
import com.ruoyi.article.domain.MyArticle;
import com.ruoyi.article.service.IMyArticleService;

/**
 * 文章管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-12
 */
@Service
public class MyArticleServiceImpl implements IMyArticleService {
    @Autowired
    private MyArticleMapper myArticleMapper;
    @Autowired
    private IArticleSeeService articleSeeService;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询文章管理
     *
     * @param uid 文章管理主键
     * @return 文章管理
     */
    @Override
    public MyArticle selectMyArticleByUid(String uid) {
        return myArticleMapper.selectMyArticleByUid(uid);
    }

    /**
     * 查询文章管理列表
     *
     * @param myArticle 文章管理
     * @return 文章管理
     */
    @Override
    public List<MyArticle> selectMyArticleList(MyArticle myArticle) {
        List<MyArticle> myArticleList = myArticleMapper.selectMyArticleList(myArticle);
        myArticleList.forEach((res) -> {
            SysUser user = iSysUserService.selectUserById(Long.valueOf(res.getUser()));
            res.setUserName(user.getUserName());
        });
        return myArticleList;
    }

    /**
     * 新增文章管理
     *
     * @param myArticle 文章管理
     * @return 结果
     */
    @Override
    public int insertMyArticle(MyArticle myArticle) {
        myArticle.setUid(UUID.randomUUID().toString().replace("-", "").substring(0, 8));
        myArticle.setCreateTime(DateUtils.getNowDate());
        System.out.println("uid" + myArticle.getUid());
        return myArticleMapper.insertMyArticle(myArticle);
    }

    /**
     * 修改文章管理
     *
     * @param myArticle 文章管理
     * @return 结果
     */
    @Override
    public int updateMyArticle(MyArticle myArticle) {
        myArticle.setUpdateTime(DateUtils.getNowDate());
        return myArticleMapper.updateMyArticle(myArticle);
    }

    /**
     * 批量删除文章管理
     *
     * @param uids 需要删除的文章管理主键
     * @return 结果
     */
    @Override
    public int deleteMyArticleByUids(String[] uids) {
        return myArticleMapper.deleteMyArticleByUids(uids);
    }

    @Override
    public int editMyArticleByObj(MyArticle[] myArticles) {

        return 1;
    }

    /**
     * 删除文章管理信息
     *
     * @param uid 文章管理主键
     * @return 结果
     */
    @Override
    public int deleteMyArticleByUid(String uid) {
        return myArticleMapper.deleteMyArticleByUid(uid);
    }

    @Override
    public MyArticle getId(String uid) {
        MyArticle myArticle = myArticleMapper.selectMyArticleByUid(uid);
//        ArticleSee articleSee = new ArticleSee();
//        articleSee.setArticleId(uid);
//        articleSeeService.insertArticleSee(articleSee);
        if (myArticle.getSeeNum() == null) {
            myArticle.setSeeNum(1L);
        } else {
            myArticle.setSeeNum(myArticle.getSeeNum() + 1);
        }
        myArticleMapper.updateMyArticle(myArticle);
        return myArticle;
    }

    @Override
    public int updateMyArticleUrls(List<MyArticle> myArticle) {
        return myArticleMapper.updateMyArticleUrls(myArticle);
    }
}
